<?php

namespace App\Models\ModelManage;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;

/**
 * Class TableField
 * @package App\Models\ModelManage
 * @version August 7, 2018, 5:45 pm CST
 *
 * @property \App\Models\ModelManage\Table table
 * @property string name
 * @property string title
 * @property integer table_id
 * @property string field_type
 * @property string field_filter
 * @property string column_width
 * @property string rw
 * @property string form
 * @property string attr
 * @property string description
 */
class TableField extends Model
{
    use SoftDeletes;

    public $table = 'table_fields';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'title',
        'table_id',
        'field_type',
        'field_filter',
        'column_width',
        'rw',
        'form',
        'attr',
        'description',
        'type',
        'database_id', // 数据库
        'clone', // 克隆
        'publish', // 发布
        'index', // 索引 0否 1是
        'listorder',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name'         => 'string',
        'title'        => 'string',
        'table_id'     => 'integer',
        'field_type'   => 'string',
        'field_filter' => 'string',
        'column_width' => 'string',
        'rw'           => 'string',
        'form'         => 'string',
        'attr'         => 'string',
        'description'  => 'string',
        'clone'  => 'string',
        'publish'  => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name' => 'required|max:100',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function table()
    {
        return $this->hasOne(\App\Models\ModelManage\Table::class, 'id', 'table_id');
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function database()
    {
        return $this->hasOne(\App\Models\ModelManage\Database::class, 'id', 'database_id');
    }


    //根据字段生成Migration语句
    public static function changeMigration($table_field){
        $migration_str = '';
        switch ($table_field->field_type){
            case 'integer':
                $migration_str .= '$table->integer("'.$table_field->name.'")';
                break;
            case 'string':
                $migration_str .= '$table->string("'.$table_field->name.'",100)';
                break;
            case 'text':
                $migration_str .= '$table->text("'.$table_field->name.'")';
                break;
            case 'date':
                $migration_str .= '$table->date("'.$table_field->name.'")';
                break;
            case 'dateTime':
                $migration_str .= '$table->dateTime("'.$table_field->name.'")';
                break;
            case 'char':
                $migration_str .= '$table->char("'.$table_field->name.'",8)';
                break;
            case 'year':
                $migration_str .= '$table->year("'.$table_field->name.'")';
                break;
            case 'boolean':
                $migration_str .= '$table->boolean("'.$table_field->name.'")';
                break;
            case 'float':
                $migration_str .= '$table->float("'.$table_field->name.'",8,2)';
                break;
        }


        $migration_str .=  "->nullable(true)->comment('".$table_field->title.' '.$table_field->description."');\n";

        //索引
        if($table_field->index == 1){
            $migration_str .= '$table->index("'.$table_field->name.'");\n';
        }elseif($table_field->index == 2){
            $migration_str .= '$table->unique("'.$table_field->name.'");\n';
        }
        // //外键
        // if($table_field->type == 'foreign'){
        //     $table->foreign('user_id')->references('id')->on('users');
        // }


        return $migration_str;

    }

    //生成关系表Migration
    public static function creatMapRelationTableMigration($main_table,$real_table){
        if(substr($main_table->name, -1)=='s'){
            $main_table_name = substr($main_table->name, 0,-1);
        }

        if(substr($real_table->name, -1)=='s'){
            $real_table_name = substr($real_table->name, 0,-1);
        }

        $table_name = $main_table_name.'_'.$real_table->name;
        $table_name1 = ucfirst($main_table_name).ucfirst($real_table->name);

        $php_code = <<<EOT
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class Create{$table_name1}Table extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::connection('{$main_table->database->name}')->create('{$table_name}', function(Blueprint \$table)
		{
		    \$table->increments('id');
		    \$table->integer('{$main_table_name}_id');
			\$table->integer('{$real_table_name}_id');
			});
	}
	
	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::connection('{$main_table->database->name}')->drop('{$table_name}');
	}
}
EOT;
        $file_name = 'migrations'.DIRECTORY_SEPARATOR.date("Y_m_d_His").'_create_'.$table_name.'_table.php';

        $rs = Storage::disk('local')->put($file_name, $php_code);

        if($rs){
            return storage_path('app'. DIRECTORY_SEPARATOR.$file_name);
        }else{
            return '';
        }

    }

}
